package ooday03;

public class PolymorphicDemo {
    public static void main(String[] args) {
        /*Animal a1 = new Dog("小黑", 1, "白");//向上造型，超类指向派生类
        a1.eat();//只能点Animal的内容和方法
        Swim a2 = new Dog("小黑黑", 1, "白");
        a2.swim();*/

        /*Master master = new Master();
        Dog dog = new Dog("小黑", 1, "白");
        Chick chick = new Chick("小灰", 1, "白");
        Fish fish = new Fish("小蓝", 1, "蓝");
        master.feed(dog);
        master.feed(fish);
        master.feed(chick);*/
        Animal[] animals = new Animal[3];
//        Animal animal = new Animal();//不能new对象
        animals[0] = new Dog("小黑", 1, "白");
        animals[1] = new Chick("小灰", 1, "白");
        animals[2] = new Fish("小蓝", 1, "蓝");
        for (int i = 0; i < animals.length; i++) {
            System.out.println(animals[i].name);
            animals[i].drink();
            animals[i].eat();
            if (animals[i] instanceof Dog) {
                Dog d = (Dog) animals[i];
                d.lookHome();
            }
            if (animals[i] instanceof Chick) {
                Chick c = (Chick) animals[i];
                c.layEgg();
            }
            if (animals[i] instanceof Swim) {
                Swim s = (Swim) animals[i];
                s.swim();
            }
        }


        /*Animal o = new Dog();//强制类型转换，只能转对象类型和其超类或者实现的接口
        Dog d = (Dog) o ;
        Swim s = (Swim) o;
//        Fish f = (Fish) o;
        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof Animal);
        System.out.println(o instanceof Fish);*/
    }
}
